﻿#include "imodel.h"

IModel::IModel(const IModel &d)
{
    // 复制所有成员变量的值
    m_fieldCount = d.m_fieldCount;
    m_fieldValueMap = d.m_fieldValueMap;
    m_fieldEditableMap = d.m_fieldEditableMap;
}

QStringList IModel::getFieldNameList()
{
    QStringList out;
    for(int i = 0;i<fieldCount();++i)
    {
        out<<getFieldNameByCol(i);
    }
    return out;
}

void IModel::setFieldBgColor(const QString& field, const QString &color) {
    m_backgroundField.insert(field, color);
}

QString IModel::getFieldBgColor(const QString &field) {
    if(!m_backgroundField.contains(field))
        return "";
    return m_backgroundField[field];
}

int IModel::getColByFieldName(const QString &name)
{
    for(int i = 0; i < this->fieldCount(); ++i)
    {
        if(name == this->getFieldNameByCol(i))
        {
            return i;
        }
    }
    return -1;
}

QString IModel::variant2String(QVariant v)
{
    if (v.canConvert<QString>()) {
        return v.toString();
    }
    return QString();
}

QMap<int, QString> IModel::getValueDesMap(const QString &fieldName)
{
    if(m_fieldValueMap.contains(fieldName))
        return m_fieldValueMap[fieldName];
    return QMap<int,QString>();
}

QMap<int, QString> IModel::getValueDesMap(const QString &fieldName, bool bupdate)
{
    if(m_fieldValueMap.contains(fieldName))
        return m_fieldValueMap[fieldName];
    return QMap<int,QString>();
}

QMap<QString,bool> IModel::getEditableFieldMap()
{
    return m_fieldEditableMap;
}

int IModel::fieldCount() const
{
    return m_fieldCount;
}

void IModel::setFieldCount(int newFieldCount)
{
    if (m_fieldCount == newFieldCount)
        return;
    m_fieldCount = newFieldCount;
    emit fieldCountChanged();
}

QMap<quint32, QSharedPointer<IModel> > IModel::getChildMap() const
{
    return m_childMap;
}

bool IModel::isFieldEditable(const QString &field) const
{
    if(m_fieldEditableMap.contains(field))
        return m_fieldEditableMap[field];
    return true;
}

QMap<QString, QMap<int, QString> > IModel::getFieldValueMap() const
{
    return m_fieldValueMap;
}

bool IModel::fieldEnPluginEdit(const QString &field) {
    if(!m_enPluginEdit.contains(field))
        return false;
    return m_enPluginEdit[field];
}

QString IModel::fieldPluginName(const QString &field)
{
    if(!m_fieldPluginMap.contains(field))
        return "";
    return m_fieldPluginMap[field];
}

bool IModel::fieldTextShow(const QString &field)
{
    return m_showField.contains(field) ? m_showField.value(field) : true;
}

bool IModel::fieldIsEnBg(const QString &field)
{
    return m_backgroundField.contains(field);
}

QString IModel::fieldBg(const QString &field)
{
    return m_backgroundField.contains(field) ? m_backgroundField.value(field) : "#00000";
}

